<?php
/**
 * jishupu-cms
 * @Author:技术铺<jishupu@qq.com>
 * @Date:2022/11/7
 */


namespace App\Casts;

use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Support\Facades\Storage;

/**
 * 编辑器内容类型转换：图片域名替换
 * Class Ckeditor
 * @package App\Casts
 */
class Ckeditor implements CastsAttributes
{
    /**
     * 将取出的数据进行转换
     *
     * @param \Illuminate\Database\Eloquent\Model $model
     * @param string $key
     * @param string $value
     * @param array $attributes
     * @return array
     */
    public function get($model, $key, $value, $attributes)
    {
        // 文件域名变量替换
        if (!empty($value)) {
            $configDic = [
                '__ck_uploadFileDomain__' => Storage::url(''), // 替换文件域名变量
            ];
            $replacePattern = [];
            $replaceContent = [];
            foreach ($configDic as $k => $v) {
                $replacePattern[] = $k;
                $replaceContent[] = $v;
            }
            $value = str_replace($replacePattern, $replaceContent, $value);
        }
        return $value;
    }

    /**
     * 转换成将要进行存储的值
     *
     * @param \Illuminate\Database\Eloquent\Model $model
     * @param string $key
     * @param string $value
     * @param array $attributes
     * @return string
     */
    public function set($model, $key, $value, $attributes)
    {
        return $value;
    }
}
